/*
 * @Description:
 * @Version:
 * @Autor: ps
 * @Date: 2022-07-04 13:17:53
 * @LastEditors: ps
 * @LastEditTime: 2022-10-27 15:35:47
 */
import Cookies from 'js-cookie'

const state = {
  // 载入计数
  loadingCount: 0,
  loading:false,
  sidebar: {
    opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
    withoutAnimation: false,
    hide: false
  },
  device: 'desktop',
  size: Cookies.get('size') || 'mini',
  sizeFont: '' || '16px'
}

const mutations = {
  SET_LOADING: (state, loading) => {
    state.loading = true
    state.loading = loading
  },
  /* 更新请求线程池 动态计数loading窗口*/
  updateApiCount(state, handle) {
    if (handle === "add") {
      state.loadingCount++;
      this.dispatch("app/openLoading");
    } else {
      state.loadingCount--;
      if (state.loadingCount <= 0) {
        this.dispatch("app/closeLoading");
      }
    }
  },
  TOGGLE_SIDEBAR: state => {
    if (state.sidebar.hide) {
      return false;
    }
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
    if (state.sidebar.opened) {
      Cookies.set('sidebarStatus', 1)
    } else {
      Cookies.set('sidebarStatus', 0)
    }
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set('sidebarStatus', 0)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  },
  SET_SIZE: (state, size) => {
    state.size = size
    if(size==='mini'){
      state.sizeFont='12px'
    }else if(size==='small'){
      state.sizeFont='14px'
    }else{
      state.sizeFont='16px'
    }
    Cookies.set('sizeFont', state.sizeFont)
    Cookies.set('size', size)
  },
  SET_SIDEBAR_HIDE: (state, status) => {
    state.sidebar.hide = status
  }
}

const actions = {
  toggleSideBar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  },
  setSize({ commit }, size) {
    commit('SET_SIZE', size)
  },
  toggleSideBarHide({ commit }, status) {
    commit('SET_SIDEBAR_HIDE', status)
  },
  closeLoading({ commit }) {
    commit('SET_LOADING', false)
  },
  openLoading({ commit }) {
    commit('SET_LOADING', true)
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
